﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NopSolutions.NopCommerce.BusinessLogic.Stores;
using NopSolutions.NopCommerce.BusinessLogic.SEO;
using NopSolutions.NopCommerce.BusinessLogic.Media;
using NopSolutions.NopCommerce.BusinessLogic.Configuration.Settings;
using NopSolutions.NopCommerce.BusinessLogic.Infrastructure;

namespace NopSolutions.NopCommerce.Web
{
    public partial class StoresPage : BaseNopFrontendPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SEOHelper.RenderTitle(this, GetLocaleResourceString("PageTitle.Manufactures"), true);
            if(!IsPostBack)
            {
                dlStores.DataSource = this.StoreService.GetAllStores();
                dlStores.DataBind();
            }
        }

        protected void DlStores_OnItemDataBound(object sender, DataListItemEventArgs e)
        {
            if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var store = e.Item.DataItem as Store;
                string storeURL = SEOHelper.GetStoreUrl(store);

                var hlImageLink = e.Item.FindControl("hlImageLink") as HyperLink;
                if(hlImageLink != null)
                {
                    hlImageLink.ImageUrl = this.PictureService.GetPictureUrl(store.PictureID, this.SettingManager.GetSettingValueInteger("Media.Manufacturer.ThumbnailImageSize", 125), true);
                    hlImageLink.NavigateUrl = storeURL;
                    hlImageLink.ToolTip = String.Format(GetLocaleResourceString("Media.Manufacturer.ImageLinkTitleFormat"), store.LocalizedName);
                    hlImageLink.Text = String.Format(GetLocaleResourceString("Media.Manufacturer.ImageAlternateTextFormat"), store.LocalizedName);
                }

                var hlStore = e.Item.FindControl("hlStore") as HyperLink;
                if(hlStore != null)
                {
                    hlStore.NavigateUrl = storeURL;
                    hlStore.ToolTip = String.Format(GetLocaleResourceString("Media.Manufacturer.ImageLinkTitleFormat"), store.LocalizedName);
                    hlStore.Text = Server.HtmlEncode(store.LocalizedName);
                }
            }
        }
    }
}
